-
Notifications
You must be signed in to change notification settings - Fork 0
Add method to parse host, port, and protocol from URL #100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Conversation
Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
|
Based on what I see in your unit tests, I would add the flawed case you described above. My naive guess would be that it defaults to |
| * @param {Object} output An object containing host, port, and secure properties. | ||
| * @returns {Object} The object containing valid host, port, and secure keys based on the input. | ||
| */ | ||
| const parseURL = (input) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As mentionned in the global comments, I think here I would handle the case of not specifying a protocol.
Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
| } | ||
|
|
||
| if (!input.startsWith(PROTOCOL_HTTP) && !input.startsWith(PROTOCOL_HTTPS)) { | ||
| input = PROTOCOL_HTTPS + input; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't you add a // in the middle here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't seem to be required by the url module to parse the input. On second thought though, I'll add it since most people expect https:// instead of just https:.
| let url; | ||
| try { | ||
| url = new URL(input); | ||
| } catch (e) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be useful to log a warning, you think?
Overview
Normally, when we specify the host of an API, we do so like this:
If we specify both the protocol and the port in the
hostvariable, it makes the sdk behave in unexpected ways. For example:All the requests would go to
https://http://example.com:1234:443.Using Node's
urlmodule, we parse the url, and if a protocol/port are found, we override the default ones.This change is made with backward compatibility in mind. If the host we provide is just the hostname, the sdk behaves like normal.
Flaws
If we specify a host with a port but not a protocol:
Node's url parser thinks the protocol is
example.com:and that the hostname is'', which would default tolocalhost.Todo
secureflag?